package cn.newtest.common.dbutils.dialect;

/**
 * Oracle采用嵌套3层的查询语句结合rownum来实现分页，这在Oracle上是最快的方式，
 * 如果只是一层或者两层的查询语句的rownum不能支持order by。
 * 
 * @author WangChui(baiyibing@gmail.com)
 * @version 1.0 TODO
 * 
 */
public class Oracle9Dialect implements DBDialect {
	public boolean supportsLimit() {
		return true;
	}

	public String getCountString(String sql) {
		StringBuffer countSelect = new StringBuffer(100);
		countSelect.append("select count(*) from (");
		countSelect.append(sql);
		countSelect.append(" ) totalTable ");
		return countSelect.toString();
	}

	public String getLimitString(String sql) {
		StringBuffer pagingSelect = new StringBuffer(100);
		pagingSelect
				.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(sql);
		pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");
		return pagingSelect.toString();
	}

	public String getLimitString(String sql, int begin, int end, String orderByKey) {
		StringBuffer pagingSelect = new StringBuffer(100);
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		pagingSelect.append(sql);
		pagingSelect.append(" ) row_ where rownum <= " + end
				+ ") where rownum_ > " + begin);
		return pagingSelect.toString();
	}
}
